<template>
  <div class="top-menu">
    <el-menu :default-active="activeIndex"
             mode="horizontal"
             text-color="#333">
      <template v-for="(item,index) in items">
        <el-menu-item :index="item.id+''"
                      @click.native="openMenu(item)"
                      :key="index">
          <template slot="title">
            <i :class="item.icon"></i>
            <span>{{generateTitle(item)}}</span>
          </template>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "top-menu",
  data() {
    return {
      activeIndex: "0",
      items: []
    };
  },
  created() {
    this.getMenu();
  },
  computed: {
    ...mapGetters(["tagCurrent", "menu"])
  },
  methods: {
    getMenu() {
      let _this = this
      _this.$store.dispatch("GetTopMenu").then(res => {
        _this.items = res;
        if (_this.items.length > 0) {
          _this.openMenu(_this.items[0])
        }
      });
    },
    generateTitle(item) {
      return this.$router.$avueRouter.generateTitle(
        item.title,
        (item.meta || {}).i18n
      );
    },
    openMenu(item) {
      this.$store.dispatch("GetMenu", item.id).then(data => {
        if (data.length !== 0) {
          this.$router.$avueRouter.formatRoutes(data, true);
        }
        // let itemActive,
        //   childItemActive = 0;
        // if (item.path) {
        //   itemActive = item;
        // } else {
        //   console.log(this.menu);
        //   if (this.menu[childItemActive].length == 0) {
        //     itemActive = this.menu[childItemActive];
        //   } else {
        //     itemActive = this.menu[childItemActive].sub[childItemActive];
        //   }
        // }
        // this.$router.push({
        //   path: this.$router.$avueRouter.getPath({
        //     name: itemActive.title,
        //     src: itemActive.path,
        //     id: itemActive.id,
        //     i18n: (itemActive.meta || {}).i18n
        //   })
        // });
      });
    }
  }
};
</script>